package com.shu.io;

import com.shu.jdk8.CloneTest;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.ToString;
import lombok.experimental.Accessors;
import lombok.extern.slf4j.Slf4j;

import java.io.*;

/**
 * '
 *
 * @author ssm
 * @version V1.0
 * @description:
 * @date 2020/4/13 10:32
 */
@Accessors(chain = true)
@Slf4j
@Data
@AllArgsConstructor
@NoArgsConstructor
@ToString
public class Car implements Serializable,Cloneable {
    private static final long serialVersionUID = -3186469725642569977L;

    private String name;
    private transient String password; // 拖拉机启动密码
    private String price;

    // 序列化
    private void writeObject(java.io.ObjectOutputStream out) throws IOException {
        log.info("writeObject");
        // out.defaultWriteObject(); // 默认序列化方法
        out.writeObject(name);
        out.writeObject(price);

        // 演示代码暂未加密,使用逆转
        out.writeObject(new StringBuffer(password).reverse());
    }

    // 反序列化
    private void readObject(java.io.ObjectInputStream in) throws IOException, ClassNotFoundException {
        log.info("readObject");
        // in.defaultReadObject(); // 默认反序列化方法
        this.name = (String) in.readObject();
        this.price = (String) in.readObject();

        // 演示代码暂未加密,使用逆转
        this.password = new StringBuilder(String.valueOf(in.readObject())).reverse().toString();
    }

    @Override
    public Car clone() throws CloneNotSupportedException {
        return (Car)super.clone();
    }




}
